﻿
Imports System.Data.Linq


Public Class LinqToSqlRepository(Of T As Class)
    Implements IRepository(Of T)

    Private context As DataContext

    Public Function Add(ByVal entity As T) As T Implements IRepository(Of T).Add
        context.GetTable(Of T)().InsertOnSubmit(entity)
        Return entity
    End Function

    Public Sub Attach(ByVal current As T) Implements IRepository(Of T).Attach
        context.GetTable(Of T).Attach(current)
    End Sub

    Public Sub Attach(ByVal current As T, ByVal original As T) Implements IRepository(Of T).Attach
        context.GetTable(Of T).Attach(current, original)
    End Sub

    Public Function Query() As System.Linq.IQueryable(Of T) Implements IRepository(Of T).Query
        Return context.GetTable(Of T)()
    End Function

    Public Sub Remove(ByVal entity As T) Implements IRepository(Of T).Remove
        context.GetTable(Of T).DeleteOnSubmit(entity)
    End Sub

    Public Sub SubmitChanges() Implements IRepository(Of T).SubmitChanges
        context.SubmitChanges()

    End Sub

    Public Sub New(ByVal ctxt As DataContext)
        context = ctxt
    End Sub
End Class



